/**
 * 获取省市区名称
 * @param areaData -省市区数据
 * @param codes -code
 * @return {*[]}
 */

export const getAreaLabelsByCode = (areaData, codes, value = "value", label = "label") => {
  if (!Array.isArray(areaData)) {
    return new Error("areaData must be array");
  }
  if (!Array.isArray(codes)) {
    return new Error("codes must be array");
  }
  const labels = [];
  const codeSet = new Set(codes.map(code => Number(code)));
  const inverseAnalysis = areaData => {
    for (const item of areaData) {
      if (codeSet.has(Number(item[value]))) {
        labels.push(item[label]);
        codeSet.delete(Number(item[value]));
        if (codeSet.size === 0) return;
      }
      if (item.children?.length) {
        inverseAnalysis(item.children);
        if (codeSet.size === 0) return;
      }
    }
  };
  inverseAnalysis(areaData);
  return labels;
};
